import { useCurd, useForm, useTable } from '@/hooks/useCurd'
import { PaginationDto } from '@/modules/common/common.dto'
import { Injectable } from 'butterfly-decorators'
import { ApplicationController } from '../application/application.controller'
import { TableController } from '../table/table.controller'
import { Module, ModuleController } from './module.controller'

export const tableState = useTable<Module>()
export const formState = useForm<Module>()

export const init = () => {
  tableState.columns = [
    { type: 'selection' },
    { title: '模块名称', key: 'moduleName' },
    { title: '模块描述', key: 'moduleDesc' }
  ]
  formState.template = [
    {
      type: 'select',
      label: '关联应用',
      keyPath: 'applicationId',
      selectProps: {
        options: [],
        placeholder: '请选择应用',
        onChange(value) {
          console.log(value)
          ModuleService.getTableList(value)
        }
      }
    },
    {
      type: 'select',
      label: '关联表',
      keyPath: 'tableId',
      selectProps: {
        options: [],
        placeholder: '请选择表'
      }
    },
    {
      type: 'text',
      label: '文件夹名称',
      keyPath: 'moduleDir',
      textProps: { placeholder: '请输入模块文件夹名称（禁止中文）' }
    },
    {
      type: 'text',
      label: '模块名称',
      keyPath: 'moduleName',
      textProps: { placeholder: '请输入模块名称' }
    },
    {
      type: 'textarea',
      label: '模块描述',
      keyPath: 'moduleDesc',
      textareaProps: { placeholder: '请输入模块描述' }
    }
  ]
}

@Injectable({
  controllers: [ModuleController, ApplicationController, TableController]
})
export class ModuleService {
  static moduleController: ModuleController
  static applicationController: ApplicationController
  static tableController: TableController

  static async getApplicationList() {
    const { code, data } = await this.applicationController.findListReq()!
    if (code == 200) {
      formState.template![0].selectProps!.options = data.list.map((item) => ({
        label: item.applicationName,
        value: item.id
      }))
    }
  }

  static async getTableList(applicationId: number) {
    const { code, data } = await this.tableController.findFilterReq({}, { applicationId })!
    console.log(data)
    if (code == 200) {
      formState.template![1].selectProps!.options = data.list.map((item) => ({
        label: item.tableName,
        value: item.id
      }))
    }
  }

  static async getList({ page, size }: PaginationDto) {
    const res = await this.moduleController.findListReq({ page, size })!
    if (res.code === 200) {
      return { list: res.data.list, total: res.data.pageInfo.total }
    }
    return { list: [], total: 0 }
  }

  static async createOne(newData: Module) {
    const res = await this.moduleController.addOneReq(newData)!
    if (res.code == 200) {
      return { success: true, msg: '操作成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }

  static async updateOne(newData: Module) {
    const res = await this.moduleController.updateOneReq(newData)!
    if (res.code === 200) {
      return { success: true, msg: '操作成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }

  static async deleteByIds(ids: any[]) {
    const res = await this.moduleController.deleteByIdsReq(ids)!
    if (res.code === 200) {
      return { success: true, msg: '删除成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }
}

export const { r, c, u, d } = useCurd({
  r(page, size) {
    return ModuleService.getList({ page, size })
  },
  c(newData) {
    return ModuleService.createOne(newData)
  },
  u(newData) {
    return ModuleService.updateOne(newData)
  },
  d(keys) {
    return ModuleService.deleteByIds(keys)
  }
})
